package com.javabasic;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Unit19TimeDemo {
	public static void main(String [] args) {
		// Get time by Millis
		long start = System.currentTimeMillis();
		try {
			for (int i = 0; i < 1000; i++) {
//				Thread.sleep(2);
			}
		} catch (Exception e) {
			
		}
		long end = System.currentTimeMillis();
		
		System.out.println("Millis: " + (end - start));
		
		// Get time by Nano
		start = System.nanoTime();
		
		end = System.nanoTime();
		System.out.println("Nano: " + (end - start));
		
		// Display the system time
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar rightnow = Calendar.getInstance();
		
		Calendar birthdayYear = Calendar.getInstance();
		birthdayYear.set(1990, 0, 21);
		
		String day = dateFormat.format(rightnow.getTime());
		String birthday = dateFormat.format(birthdayYear.getTime());
		
		System.out.println("System time: " + day);
		System.out.println("System time: " + birthdayYear.get(Calendar.YEAR));
		
		// Convert from string to Date
		try {
			Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2013-12-21");
			System.out.println(dateFormat.format(date));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		// Set time by Calendar and GregorianCalendar
		Calendar calen = new GregorianCalendar(1990, 00, 21);
		System.out.println(dateFormat.format(calen.getTime()));
		
	}
	
}
